package com.stnet.web.enterprice.base;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.modules.commons.PageAction;
import com.stnet.bases.Constants;
import com.stnet.entity.Authorities;
import com.stnet.entity.EnterpriseColumn;
import com.stnet.front.enterprice.EpColumnOut;

/**
 * @author Messcat
 * 
 */
public class EpColumnAction extends PageAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	private List<EpColumnOut> epColumnOutList;
	private EnterpriseColumn enterpriseColumn;
	private List<EnterpriseColumn> enterpriseColumnList;
	private String names;
	private String shortName;
	private String num;
	private String intro;
	private Long orderColumn;
	private String linkUrl;
	private String typeColumn;
	private String frontNum;
	private String numValidInRight;
	private String numValidInLeft;
	private Long id;
	
	private  String nametemp;
	
	private Long father;
	
	private String columnName;
	
	public EpColumnAction() {
	}

	/**
	 * 异步验证栏目名不能为重复
	 * @return
	 * @throws Exception
	 */
	public String checkName() throws Exception {
		
		String event = "";
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String names = request.getParameter("names");
		String orgName = request.getParameter("orgName");
		names = URLDecoder.decode(names, "UTF-8");
		orgName = URLDecoder.decode(orgName, "UTF-8");
		
		if (epColumnManagerDao.isNameUnique(names, orgName,Long.valueOf(columnName)))
			event = "true";
		else
			event = "false";
		
		return renderText(event);
	}

	public String execute() throws Exception {
		setEpColumnOutList(epColumnManagerDao.findByFatherAndState());
		return Constants.SUCCESS_KEY;
	}

	/**
	 * 查看栏目信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		setEnterpriseColumn(epColumnManagerDao.getEnterpriseColumn(id));
		return Constants.VIEW_KEY;
	}

	/**
	 * 添加栏目信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		
		enterpriseColumn.setId(null);
		initEpColumn(enterpriseColumn);
		epColumnManagerDao.saveEnterpriseColumn(enterpriseColumn);
		
		this.father = 0L;
		return this.query();
	}

	/**
	 * 修改栏目信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		initEpColumn(enterpriseColumn);
		EnterpriseColumn temp = this.epColumnManagerDao.getEnterpriseColumn(enterpriseColumn.getId());
		Authorities authorities = this.authoritiesManagerDao.getByName(temp.getNames());
		authorities.setDisplayName(enterpriseColumn.getNames());
		authorities.setName(enterpriseColumn.getNames());
		
		epColumnManagerDao.updateEnterpriseColumn(enterpriseColumn,authorities);
		this.father = 0L;
		return this.query();
	}

	/**
	 * 跳转到添加栏目页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add_page() throws Exception {
		setEpColumnOutList(epColumnManagerDao.findByFatherAndState());
		return Constants.ADD_PAGE_KEY;
	}

	/**
	 * 跳转到编辑栏目页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		setEnterpriseColumn(epColumnManagerDao.getEnterpriseColumn(id));
		setEnterpriseColumnList(epColumnManagerDao.findEnterpriseColumn());
		return Constants.EDIT_KEY;
	}

	/**
	 * 删除栏目信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		epColumnManagerDao.deleteEnterpriseColumn(id);
		this.father = 0L;
		return this.query();
	}

	/**
	 * 查询栏目信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String query() throws Exception {
		
		String event = "";
		this.setEnterpriseColumnList(this.epColumnManagerDao.findTreeByFather(father));
		/**
		 * 如果栏目没有子栏目，则跳转到本栏目编辑页面
		 * */
		if (this.getEnterpriseColumnList()!=null && this.getEnterpriseColumnList().size() < 1) {
			this.id = father;
			event = this.edit();
		}else{
			event = Constants.SUCCESS_KEY;
		}
		return event;
	}

	/**
	 * 初始化对象信息
	 * @param enterpriseColumn
	 */
	private void initEpColumn(EnterpriseColumn enterpriseColumn) {
		enterpriseColumn.setNames(names);
		enterpriseColumn.setShortName(shortName);
		enterpriseColumn.setNum(num);
		enterpriseColumn.setIntro(intro);
		enterpriseColumn.setOrderColumn(orderColumn);
		enterpriseColumn.setLinkUrl(linkUrl);
		enterpriseColumn.setTypeColumn(typeColumn);
		enterpriseColumn.setFrontNum(frontNum);
	}
	
	public List getEpColumnOutList() {
		return epColumnOutList;
	}

	public void setEpColumnOutList(List epColumnOutList) {
		this.epColumnOutList = epColumnOutList;
	}

	public EnterpriseColumn getEnterpriseColumn() {
		return enterpriseColumn;
	}

	public void setEnterpriseColumn(EnterpriseColumn enterpriseColumn) {
		this.enterpriseColumn = enterpriseColumn;
	}

	public List getEnterpriseColumnList() {
		return enterpriseColumnList;
	}

	public void setEnterpriseColumnList(List enterpriseColumnList) {
		this.enterpriseColumnList = enterpriseColumnList;
	}

	public String getNames() {
		return names;
	}

	public void setNames(String names) {
		this.names = names;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOrderColumn() {
		return orderColumn;
	}

	public void setOrderColumn(Long orderColumn) {
		this.orderColumn = orderColumn;
	}

	public String getLinkUrl() {
		return linkUrl;
	}

	public void setLinkUrl(String linkUrl) {
		this.linkUrl = linkUrl;
	}

	public String getTypeColumn() {
		return typeColumn;
	}

	public void setTypeColumn(String typeColumn) {
		this.typeColumn = typeColumn;
	}

	public String getFrontNum() {
		return frontNum;
	}

	public void setFrontNum(String frontNum) {
		this.frontNum = frontNum;
	}

	public String getNumValidInRight() {
		return numValidInRight;
	}

	public void setNumValidInRight(String numValidInRight) {
		this.numValidInRight = numValidInRight;
	}

	public String getNumValidInLeft() {
		return numValidInLeft;
	}

	public void setNumValidInLeft(String numValidInLeft) {
		this.numValidInLeft = numValidInLeft;
	}

	public Long getFather() {
		return father;
	}

	public void setFather(Long father) {
		this.father = father;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getNametemp() {
		return nametemp;
	}

	public void setNametemp(String nametemp) {
		this.nametemp = nametemp;
	}
}